<template>
  <div v-loading="loading" class="game_banner">
    <div class="game_banner_bg">
      <img :src="getStaticFileUrl(gameInfo.backgroundImage)" alt="">
    </div>
    <div class="game_content">
      <div class="game_logo_name">
        <img class="game_logo" :src="getStaticFileUrl(gameInfo.logoImage)" alt="">
        <span class="game_name gl_db_ellipsis">
          {{ gameInfo.parent ? gameInfo.parent.name + '-' : '' }}{{ gameInfo.name }}
        </span>
      </div>
      <div class="game_detaile">
        <div class="left_detaile_message">
          <div class="game_detaile_item">
            <span>报名时间：</span>
            <span v-if="gameInfo.entryStartTime">{{ gameInfo.entryStartTime | FormatDate('yyyy年MM月dd日') }} ~ {{ gameInfo.entryEndTime | FormatDate('yyyy年MM月dd日') }}</span>
          </div>
          <div class="game_detaile_item">
            <span>比赛时间：</span>
            <span v-if="gameInfo.startTime">{{ gameInfo.startTime | FormatDate('yyyy年MM月dd日') }} ~ {{ gameInfo.endTime | FormatDate('yyyy年MM月dd日') }}</span>
          </div>
          <div class="game_detaile_item">
            <span>赛事级别：</span>
            <span>{{ gameInfo.level && gameInfo.level.name }}</span>
          </div>
          <div class="game_detaile_item">
            <span>赛事地址：</span>
            <span>{{ gameInfo.address }}</span>
          </div>
          <div class="game_entry_btn">
            <div
              v-if="gameInfoStatusCode === 'EntryStarted' && gameInfo.approveStatusCode === 'ApproveSuccess' && gameInfo.isPublished && isShowEntryBtn"
              class="entry_btn gl_mr_10"
              @click="clickJump('Enter')"
            >
              立即报名
            </div>
            <div
              v-if="gameInfo.approveStatusCode === 'ApproveSuccess' && gameInfo.isPublished && isShowLogistic"
              class="entry_btn"
              @click="clickJump('Logistics')"
            >
              后勤服务
            </div>
          </div>
          <div v-if="gameInfo.entryNotice || gameInfo.disclaimerNotice || gameInfo.regulation" class="game_rules">
            赛事相关条例：
            <a v-if="gameInfo.entryNotice" class="download_file" @click="downloadFile(gameInfo.entryNotice, '报名须知')">《报名须知》</a>
            <a v-if="gameInfo.disclaimerNotice" class="download_file" @click="downloadFile(gameInfo.disclaimerNotice, '免责声明')">《免责声明》</a>
            <a v-if="gameInfo.regulation" class="download_file" @click="downloadFile(gameInfo.regulation, '竞赛规程')">《竞赛规程》</a>
          </div>
        </div>
        <div class="right_qr_code">
          <div id="qrcode" class="qr_code" />
          <span class="qr_code_message">微信扫码在小程序报名</span>
        </div>
      </div>
    </div>
    <div class="cust">
      <el-button type="success" icon="el-icon-phone-outline" round @click="()=>{visible='ture'}">联系客服</el-button>
    </div>
    <el-dialog title="联系客服" width="30%" :visible.sync="visible">
      <div class="customerImg">
        <img class="customer" src="@/assets/images/customer.png" alt="">
      </div>
    </el-dialog>
  </div>
</template>
<script>
import axios from 'axios';
import { getToken } from '@/lib/auth';
import { errorText } from '@/lib';
import {
  api_GET_EventsManagementCompetitionWithRoute,
  api_GET_EventsManagementLogisticEntryLogisticSetings // 获取赛事后勤设置的模块信息
} from '@/extend/api/gameswaggerApi/app';
import {
  Api_GET_EventsManagementLogisticEntryLogisticSetingsQO
} from '@/lib/form/game/swaggerForm';
import QRCode from 'qrcodejs2';
console.log('WEB_CONFIG', WEB_CONFIG);
export default {
  name: 'GameBanner',
  props: {
    isShowEntryBtn: {
      type: Boolean,
      default: true
    }
  },
  data() {
    return {
      visible: false,
      isDisabled: false,
      baseUrl: `${WEB_CONFIG.VUE_APP_SWAGGER_GAME_BASE}`,
      fileUrl: `${WEB_CONFIG.VUE_APP_STATIC_FILE_RUL}`,
      tokenInfo: getToken(),
      loading: false,
      isShowLogistic: false,
      LogisticQO: new Api_GET_EventsManagementLogisticEntryLogisticSetingsQO(),
      gameInfo: {},
      wxQrCodeBaseUrl: `${WEB_CONFIG.VUE_APP_API_WXQRCODE}`
    };
  },
  computed: {
    gameInfoStatusCode () {
      const _gameInfo = this.gameInfo.status || {};
      return _gameInfo.code;
    }
  },
  created() {
    const $route = this.$route;
    const _paramsId = $route.params.id;
    this.gameRoute = _paramsId.split('_')[0];
    // 因为短域名原因手动添加VUE_APP_Get_TenantId
    this.tenantId = process.env.VUE_APP_Get_TenantId;
    this.getGameInfoByGameRoute();
  },
  mounted() {
    this.createdQrCode();
  },
  methods: {
    // 生成二维码
    createdQrCode() {
      const _this = this;
      new QRCode('qrcode', {
        width: 160,
        height: 160,
        text: `${_this.wxQrCodeBaseUrl}?a=game_${_this.$route.params.id}`
        // text: `${location.origin}/mobile.html#/home/${_this.$route.params.id}/info`
      });
      // https://manage-test.wtwuxicenter.com/wx_qrcode?a=game_mfss
    },
    // 根据赛事路由获取赛事信息
    getGameInfoByGameRoute() {
      const gameQO = {};
      gameQO.route = this.gameRoute;
      gameQO.IncludeStatus = true;
      gameQO.IncludeLevel = true;
      gameQO.IncludeSetting = true;
      gameQO.IncludeParent = true;
      if (this.tenantId) {
        gameQO.__tenant = this.tenantId;
      }
      this.loading = true;
      api_GET_EventsManagementCompetitionWithRoute(gameQO).then(res => {
        if (res.isDisabled) {
          this.$parent.isDisabled = true;
        } else {
          this.$parent.isDisabled = false;
        }
        this.gameInfo = res || {};
        this.loading = false;
        this.getLogisticSetings(this.gameInfo.id);
        this.gameId = this.gameInfo.id;
        this.$emit('getGameInfoSuccess', this.gameInfo);
      });
    },
    // 获取该赛事的后勤设置选项
    getLogisticSetings(id) {
      var that = this;
      that.loading = true;
      that.LogisticQO.providerKey = id;
      api_GET_EventsManagementLogisticEntryLogisticSetings(that.LogisticQO).then(res => {
        const list = res || [];
        that.isShowLogistic = list.length > 0 && !that.$route.query.id;
        that.loading = false;
      }, (error) => {
        that.loading = false;
        that.$message({ type: 'error', message: error.error.message || '获取后勤设置失败' });
      });
    },
    // 获取文件地址
    getStaticFileUrl(relativeUrl) {
      if (!relativeUrl) return '';
      return WEB_CONFIG.VUE_APP_STATIC_FILE_RUL + relativeUrl;
    },
    // 处理背景图
    handleBackgroundImg(relatvieUrl) {
      return `url('${this.getStaticFileUrl(relatvieUrl)}');`;
    },
    // 点击跳转页面
    clickJump(name) {
      this.$router.push({
        name: name,
        query: { id: this.gameId }
      });
    },
    downloadFile(fileUrl, fileName) {
      axios({
        methods: 'get',
        url: `${this.fileUrl}${fileUrl}`,
        responseType: 'blob'
      }).then(res => {
        var blob = new Blob([res.data]);
        const suffix = fileUrl.split('.')[fileUrl.split('.').length - 1];
        if ('msSaveOrOpenBlob' in navigator) {
          window.navigator.msSaveOrOpenBlob(blob, fileName + '.' + suffix);
          return;
        }
        const downloadElement = document.createElement('a');
        const href = window.URL.createObjectURL(res.data);
        downloadElement.style.display = 'none';
        downloadElement.href = href;
        // downloadElement.download = fileName + '.' + suffix;
        document.body.appendChild(downloadElement);
        downloadElement.click();
        document.body.removeChild(downloadElement);
        window.URL.revokeObjectURL(href);
      }).catch(error => {
        errorText(error.response.data).then(function(text) {
          that.$message({ type: 'error', message: text });
        });
      });
    }
  }
};
</script>
<style scoped>
.game_banner {
  position: relative;
  width: 1200px;
  min-height: 298px;
  border-radius: 8px;
  box-shadow: 0px 16px 70px 0px rgba(0,0,0,0.10);
  margin: 20px auto 20px;
  overflow: hidden;
}
.game_banner_bg {
  float: left;
  width: 555px;
  height: 298px;
  border-radius: 10px;
  overflow: hidden;
}
.game_banner_bg img {
  width: 100%;
  height: 100%;
  vertical-align: middle;
  border-radius: 8px 0 0 8px;
}
.game_content {
  position: relative;
  float: left;
  width: calc(100% - 555px);
  padding: 8px 20px 8px 20px;
  box-sizing: border-box;
}
.game_logo_name {
  display: flex;
  justify-content: center;
  align-items: center;
  align-content: center;
}
.game_logo_name .game_logo {
  width: 63px;
  height: 63px;
  margin-right: 16px;
  border-radius: 50%;
  vertical-align: middle;
}
.game_logo_name .game_name {
  width: calc(100% - 80px);
  font-size: 24px;
  line-height: 24px;
  vertical-align: middle;
}
.left_detaile_message {
  position: relative;
  float: left;
  margin-top: 21px;
  width: 392px;
}
.left_detaile_message::after {
  content: "";
  position: absolute;
  top: 38px;
  right: 0;
  background: #31456C;
  width: 2px;
  height: 120px;
}
.left_detaile_message .game_detaile_item {
  font-size: 16px;
  line-height: 24px;
  margin-bottom: 4px;
  padding-right: 10px;
  word-break:break-all;
}
.game_entry_btn {
  width: 100%;
  max-height: 68px;
  padding: 8px 0 12px 0;
  box-sizing: border-box;
}
.game_entry_btn .entry_btn {
  width: 180px;
  height: 44px;
  background: #dc1d1d;
  border-radius: 4px;
  font-size: 16px;
  line-height: 47px;
  color: #fff;
  cursor: pointer;
  text-align: center;
  transition: all 0.3s ease;
  display: inline-block;
}
.game_entry_btn .entry_btn:hover {
  opacity: 0.8;
}
.game_rules {
  font-size: 14px;
  color: #353434;
}
.game_rules .download_file {
  color: #dc1d1d;
  font-weight: 700;
}
.game_rules .download_file:hover {
  text-decoration: underline;
}
.right_qr_code {
  float: right;
  text-align: center;
  font-size: 16px;
  padding-top: 25px;
}
.right_qr_code .qr_code {
  width: 160px;
  height: 160px;
  margin-bottom: 9px;
}
.customerImg {
    /* position: fixed;
    top: 80px;
    right: 10px;
    border-radius: 1px; */
    display: flex;
    justify-content: center;
    align-items: center;
}
.cust {
    position: fixed;
    bottom: 300px;
    margin-left: 1222px;
    border-radius: 1px;
    z-index: 999;
}
.customer {
  width: 301px;
  margin: 0 auto;
}
.el-button  ::v-deep  i {
  font-size: 20px;
}
.el-button  ::v-deep  span {
  font-size: 20px;
}
</style>
